package com.jzo2o.customer.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jzo2o.common.model.CurrentUserInfo;
import com.jzo2o.common.model.PageResult;
import com.jzo2o.customer.mapper.WorkerCertificationAuditMapper;
import com.jzo2o.customer.model.domain.WorkerCertification;
import com.jzo2o.customer.model.domain.WorkerCertificationAudit;
import com.jzo2o.customer.model.dto.request.WorkerCertificationAuditAddReqDTO;
import com.jzo2o.customer.model.dto.request.WorkerCertificationAuditPageQueryReqDTO;
import com.jzo2o.customer.service.IWorkerCertificationService;
import com.jzo2o.customer.service.WorkerCertficationAuditService;
import com.jzo2o.mvc.utils.UserContext;
import com.jzo2o.mysql.utils.PageHelperUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Date;

@Service
public class WorkerCertficationAuditServiceImpl extends ServiceImpl<WorkerCertificationAuditMapper, WorkerCertificationAudit> implements WorkerCertficationAuditService {
    @Autowired
    private IWorkerCertificationService workerCertificationService;
    /**
     * 服务端提交认证申请
     */
    @Override
    public void certificationAudit(WorkerCertificationAuditAddReqDTO dto) {
        // 服务人员认证审核表
        WorkerCertificationAudit workerCertificationAudit = BeanUtil.copyProperties(dto, WorkerCertificationAudit.class);
        workerCertificationAudit.setServeProviderId(UserContext.currentUserId());
        workerCertificationAudit.setAuditStatus(0);
        workerCertificationAudit.setCertificationStatus(1);
        save(workerCertificationAudit);
        // 服务人员认证信息表
        WorkerCertification worker = BeanUtil.copyProperties(dto, WorkerCertification.class);
        worker.setCertificationStatus(1);
        worker.setCertificationTime(LocalDateTime.now());
        workerCertificationService.save(worker);
    }

    /**
     * 查询最新的驳回原因
     */
    @Override
    public String rejectReason() {
        Long userId = UserContext.currentUserId();
        WorkerCertificationAudit one = lambdaQuery().eq(WorkerCertificationAudit::getServeProviderId, userId).orderByDesc(WorkerCertificationAudit::getCreateTime).last("limit 1").one();
        return one.getRejectReason();
    }

    /**
     * 审核服务人员认证分页查询
     */
    @Override
    public PageResult<WorkerCertificationAudit> queryPageWorker(WorkerCertificationAuditPageQueryReqDTO dto) {
        //调用mapper查询数据，这里由于继承了ServiceImpl<ServeMapper, Serve>，使用baseMapper相当于使用ServeMapper
        return PageHelperUtils.selectPage(dto, () -> baseMapper.queryList(dto.getName(), dto.getIdCardNo(), dto.getAuditStatus(), dto.getCertificationStatus()));
    }

    /**
     * 审核服务人员认证信息
     */
    @Override
    public void auditServicePersonnel(Integer certificationStatus, Long id, String rejectReason) {
        CurrentUserInfo currentUserInfo = UserContext.currentUser();
        // 服务人员认证审核表
        WorkerCertificationAudit worker = baseMapper.selectById(id);
        worker.setAuditStatus(1);
        worker.setAuditorId(currentUserInfo.getId());
        worker.setAuditorName(currentUserInfo.getName());
        worker.setAuditTime(new Date());
        worker.setCertificationStatus(certificationStatus);
        worker.setRejectReason(rejectReason);
        worker.setUpdateTime(new Date());
        baseMapper.updateById(worker);
        // 服务人员认证信息表
        Long serveProviderId = worker.getServeProviderId();
        WorkerCertification workerCertification = workerCertificationService.getById(serveProviderId);
        workerCertification.setCertificationStatus(certificationStatus);
        workerCertificationService.updateById(workerCertification);
    }
}
